package wangjg.android.wifiadmin;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends BasicActivity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
    }
    
    public void onStart() {
    	this.showInfo();
    	super.onStart();
    }
    
    private void showInfo(){
		TextView ssid = (TextView)findViewById(R.id.text_ssid);
		TextView timeRange = (TextView)findViewById(R.id.text_wifi_timeRange);
		CheckBox cbEnable = (CheckBox)findViewById(R.id.checkBox_config_enable);
		TextView tvForceClose = (TextView)findViewById(R.id.text_wifi_forceClose);

		ssid.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		
    	SharedPreferences pref = this.getSharedPreferences();
    	String SSID = pref.getString("SSID","");
    	String startTime = pref.getString("startTime","");
    	String endTime = pref.getString("endTime","");
    	boolean forceClose = pref.getBoolean("forceClose", false);
    	boolean enabled = pref.getBoolean("enabled", false);
    	
    	ssid.setText(SSID);
    	timeRange.setText(startTime+"-"+endTime);
    	cbEnable.setChecked(enabled);
    	tvForceClose.setText(forceClose?R.string.YES:R.string.NOT);
    	
    	OnCheckedChangeListener listener = new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
	    		SharedPreferences pref = MainActivity.this.getSharedPreferences();
	    		Editor editor = pref.edit();
	    		editor.putBoolean("enabled", isChecked);
	    		editor.commit();
	    		
				int msgId = isChecked ? R.string.msg_enabled : R.string.msg_disabled;
				new AlertDialog.Builder(MainActivity.this)
				.setMessage(msgId)
				.setPositiveButton(R.string.button_ok, null)
				.show();
				
			}
    		
    	};
    	
		cbEnable.setOnCheckedChangeListener(listener);
		TextView tv_ssid = (TextView)findViewById(R.id.tv_label_ssid);
		tv_ssid.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				if(clickCounter.click()){
					MainActivity.this.startActivity(new Intent(MainActivity.this, EggsActivity.class));
				}
			}
		});

    }
    
    private ClickCounter clickCounter = new ClickCounter(10);
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 1, 0, R.string.menu_setup);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()){
    	case 1:
    		this.startActivity(new Intent(this, WifiAdminActivity.class));
    	}
    	return true;
    }
}
